<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/5/2
 * Time: 15:49
 */
namespace app\home\controller;

/**
 * +----------------------------------
 * Class Index
 * @package app\home\controller
 * 网站前台首页
 * +----------------------------------
 */
class Index extends Base{
    protected $tag;//标签
    protected $article_top;
    protected $type;//栏目类型
    protected $reading_article;//阅读最多的文章
    protected $config;//获取数据库blog_config配置

    /**
     * 构造函数，获取通用数据
     */
    public function _initialize(){
        $this->tag         = self::getTag();                  //获取标签
        $this->reading_article = self::getReadingArticle();    //获取阅读量最多的文章--热门文章
        $this->type        = self::type();                    //获取栏目
        $this->config      = self::getConfig();               //获取配置文件
        //trace($this->config);
    }
    /**
     * @return \think\response\View
     * 网站首页
     */
    public function index(){
        $article = self::getArticle();
        //trace($article);
        //trace($this->tag);
        //trace($this->type);
        //trace(self::link());
        return view('index',[
            'article'    => $article->items(),
            'page'       => $article->render(),
            'article_top'=> self::getTopArticle(),
            'tag'        => $this->tag,//右边标签列表
            'type'       => $this->type,//顶部导航类型
            'config'     => $this->config,//底部配置
            'reading_article' => $this->reading_article,//热门文章
            'link'       => self::link()
        ]);
    }


        /**
     * @return \think\response\View
     * 网站栏目
     */
    public function category(){
        $type_id = intval(input('param.id'));
        //trace($type_id);
        if(empty($type_id)){
            $this->error('抱歉，栏目不存在!');
        }
        $type_seo = self::typeSEO($type_id);
        //trace($type_seo);
        //判断栏目类型，根据栏目类型显示不同的模版
        //文章模型
        if($type_seo['model_id'] == 1){
            $temp = 'category';
            $article  = self::getArticle(['type_id'=>$type_id]);//获取该栏目的信息
        }
        //独立模型
        if($type_seo['model_id'] == 2){
            $temp = 'single';
        }
        //作品展示
        if($type_seo['model_id'] == 3){
            $temp = 'product';
            $article  = self::getProduct();
        }
        $artitem = $article->items();
        //trace($artitem);
        $artrend = $article->render();
        return view($temp,[
            'article'    => empty($artitem) ? [] : $article->items(),
            'page'       => empty($artrend) ? '' : $article->render(),
            'type_seo'   => $type_seo,
            'reading_article' => $this->reading_article,
            'tag'        => $this->tag,
            'type'       => $this->type,
            'config'     => $this->config,
            'type_id'    => $type_id
        ]);
    }
    /**
     * @return \think\response\View
     * 文章详情页
     */
    public function article(){
        $article_id = intval(input('param.id'));
        //trace($article_id);
        if(empty($article_id)){
            $this->error('抱歉，文章不存在!');
        }
        self::_incReading($article_id);//点击一次给文章阅读数+1
        return view('article',[
            'one'              => self::getArticleOne($article_id),//获取当前文章
            'article_tag'      => self::getArticleTag($article_id),//获取文章标签
            'previous_article' => self::getArticleOne($article_id - 1),//获取上一篇文章
            'next_article'     => self::getArticleOne($article_id + 1), //获取下一篇文章
            'related_article'  => self::getRelatedArticle(self::getArticleTag($article_id),$article_id), //获取相关文章
            'reading_article' => $this->reading_article,
            'tag'              => $this->tag,
            'type'             => $this->type,
            'config'           => $this->config
        ]);
    }

    /**
     * @return \think\response\View
     * 根据标签获取文章内容
     */
    public function tagArticle(){
        $get = input('param.');
        //trace($get);
        if(empty($get['tag_id']) && empty($get['search'])){
            $this->error('抱歉，该标签或者关键词不存在!');
        }
        $article = empty($get['search']) ? self::getTagArticle($get) : self::getArticle(['search'=>$get['search']]);
        return view('tagArticle',[
            'reading_article' => $this->reading_article,
            'tag'              => $this->tag,
            'type'             => $this->type,
            'config'           => $this->config,
            'tag_article'      => $article->items(),
            'page'             => $article->render(),
            'tag_name'         => self::getTagOne(intval($get['tag_id'])),
            'search'           => $get['search'],
        ]);
    }
    

}